/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cflp;

/**
 *
 * @author gbula
 */
public class AlgoritmoGenetico {

    private int tamPob;
    private int ngen;
    private Poblacion pobfinal;

    public AlgoritmoGenetico(int tp, int ng, double pm, double p, String nombre) {
        this.tamPob = tp;
        this.ngen = ng;
        ejecutarAlgoritmo(nombre, pm, p);
    }

    public void ejecutarAlgoritmo(String nombre, double p, double pm) {
        Network n = new Network(nombre);
        Poblacion pobInicial = new Poblacion(n, tamPob, p);
        pobfinal = new Poblacion(n, pobInicial.getPob());
        Individuo ind = new Individuo(n);
        int[] min = pobInicial.getMin();
        int cm = ind.totalCost(min);
        int j = 1;
        while (j < ngen) {
            j = j + 1;
            int[][] pf = (new OperadoresGeneticos(n, pobInicial, pm)).getPobFinal();
            pobfinal = new Poblacion(n, pf);
            min = pobfinal.getMin();
            int minj = ind.totalCost(min);
            if (minj < cm) {
                j = 0;
                cm = ind.totalCost(min);
            }
            pobInicial = new Poblacion(n, pobfinal.getPob());
        }
        System.out.println(cm);
        for (int i = 0; i < min.length; ++i) {
            System.out.print(min[i] + " ");
        }
        System.out.println("");
    }
}
